/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.retail.biz.log.controller;

import co.yixiang.common.log.annotation.Log;
import co.yixiang.common.mproot.domain.PageResult;
import co.yixiang.common.mproot.dozer.service.IGenerator;
import co.yixiang.mall.common.aop.ForbidSubmit;
import co.yixiang.retail.biz.log.service.YxStorePurchaseLogService;
import co.yixiang.retail.common.domain.YxStorePurchaseLog;
import co.yixiang.retail.common.dto.YxStorePurchaseLogDto;
import co.yixiang.retail.common.dto.YxStorePurchaseLogQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
* @author poppu_hu
* @date 2021-04-21
*/

@Api(tags = "门店进货日志管理")
@RestController
@RequestMapping("/yxStorePurchaseLog")
public class YxStorePurchaseLogController {

    private final YxStorePurchaseLogService yxStorePurchaseLogService;
    private final IGenerator generator;


    public YxStorePurchaseLogController(YxStorePurchaseLogService yxStorePurchaseLogService,IGenerator generator){
        this.yxStorePurchaseLogService=yxStorePurchaseLogService;
        this.generator=generator;
    }
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, YxStorePurchaseLogQueryCriteria criteria) throws IOException {
        yxStorePurchaseLogService.download(generator.convert(yxStorePurchaseLogService.queryAll(criteria), YxStorePurchaseLogDto.class), response);
    }

    @GetMapping
    @Log("查询门店进货日志")
    @ApiOperation("查询门店进货日志")
    public ResponseEntity<PageResult<YxStorePurchaseLogDto>> getYxStorePurchaseLogs(YxStorePurchaseLogQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxStorePurchaseLogService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增门店进货日志")
    @ApiOperation("新增门店进货日志")
    public ResponseEntity<Object> create(@Validated @RequestBody YxStorePurchaseLog resources){
        return new ResponseEntity<>(yxStorePurchaseLogService.save(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @ForbidSubmit
    @Log("修改门店进货日志")
    @ApiOperation("修改门店进货日志")
    public ResponseEntity<Object> update(@Validated @RequestBody YxStorePurchaseLog resources){
        yxStorePurchaseLogService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除门店进货日志")
    @ApiOperation("删除门店进货日志")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        Arrays.asList(ids).forEach(id->{
            yxStorePurchaseLogService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
